package org.doubango.aspect.impl;

import android.content.Context;
import android.util.Log;

import com.evendai.loglibrary.Timber;

import org.doubango.ngn.BuildConfig;
import org.doubango.ngn.services.INgnConfigurationService;
import org.doubango.ngn.utils.NgnConfigurationEntry;
import com.evendai.loglibrary.Timber;
import org.doubango.utils.SharedPreferenceUtil;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

/**
 * Created by lenovo on 2017/9/6.
 */

public class SipConfigHelper {

    private static final String TAG = SipConfigHelper.class.getCanonicalName();
    public static final String USE_SPEAKER = "USER_SPEAKER." + TAG;
    public static final String CURRENT_GROUP = "CURRENT_GROUP." + TAG;

    public static final boolean DEFAULT_USE_SPEAKER = false;
    public static final String DEFAULT_CURRENT_GROUP = "";
    public static final int DEFAULT_NETWORK_REGISTRATION_TIMEOUT = 500; //设置认证时间间隔
    public static String IDENTITY_IMPU = "";
    public static String IDENTITY_DISPLAY_NAME = "";
    public static String IDENTITY_CAMERA_GROUP = "";
    public static final String IDENTITY_PASSWORD = "123";
    public static final String NETWORK_REALM = "doubango.org";

//    static String local_ip = BuildConfig.local_ip;
//    static int local_port = BuildConfig.local_port;
//    static String net_ip = BuildConfig.net_ip;
//    static int net_port = BuildConfig.net_port;
//    static String sip_local_ip = BuildConfig.sip_local_ip;
//    static int sip_local_port = BuildConfig.sip_local_port;
//    static String sip_net_ip = BuildConfig.sip_net_ip;
//    static int sip_net_port = BuildConfig.sip_net_port;

    static String local_ip = "";
    static int local_port = 0;
    static String net_ip = "";
    static int net_port = 0;
    static String sip_local_ip = "";
    static int sip_local_port = 0;
    static String sip_net_ip = "";
    static int sip_net_port = NgnConfigurationEntry.DEFAULT_NETWORK_PCSCF_PORT;

    public static String NETWORK_PCSCF_HOST = sip_net_ip;
    public static int NETWORK_PCSCF_PORT = sip_net_port;
    public static String NETWORK_PCSCF_HOST_L = sip_local_ip;
    public static int NETWORK_PCSCF_PORT_L = sip_local_port;
    public static String NETWORK_PCSCF_HOST_N = sip_net_ip;
    public static int NETWORK_PCSCF_PORT_N = sip_net_port;

    public static String L_IP = local_ip;
    public static int L_PORT = local_port;

    public static String PROJECT_PATH = "http://" + net_ip + ":" + net_port + "/railway_emergency/";
    public static String DAZHOU_BASE_PATH = "http://" + net_ip + ":" + net_port + "/";
    public volatile static String BASE_PATH = "http://" + net_ip + ":" + net_port + "/";

    public static String PROJECT_PATH_L = "http://" + local_ip + ":" + local_port + "/railway_emergency/";
    public static String DAZHOU_BASE_PATH_L = "http://" + local_ip + ":" + local_port + "/";
    public static String BASE_PATH_L = "http://" + local_ip + ":" + local_port + "/";

    public static String PROJECT_PATH_N = "http://" + net_ip + ":" + net_port + "/railway_emergency/";
    public static String DAZHOU_BASE_PATH_N = "http://" + net_ip + ":" + net_port + "/";
    public static String BASE_PATH_N = "http://" + net_ip + ":" + net_port + "/";


    public static final String SIPSERVER_IMPU = "pttservice"; //服务器接收消息的账号

    public static final String KEY_CAMERA_GROUP = "camera_group";

    public static void setAllIPPort(String local_ip,
                                    int local_port,
                                    String net_ip,
                                    int net_port,
                                    String sip_local_ip,
                                    int sip_local_port,
                                    String sip_net_ip,
                                    int sip_net_port) {
        SipConfigHelper.local_ip = local_ip;
        SipConfigHelper.local_port = local_port;

        SipConfigHelper.net_ip = net_ip;
        SipConfigHelper.net_port = net_port;

        SipConfigHelper.sip_local_ip = sip_local_ip;
        SipConfigHelper.sip_local_port = sip_local_port;

        SipConfigHelper.sip_net_ip = sip_net_ip;
        SipConfigHelper.sip_net_port = sip_net_port;

        //初始化链接
        NETWORK_PCSCF_HOST = SipConfigHelper.sip_net_ip;
        NETWORK_PCSCF_PORT = SipConfigHelper.sip_net_port;
        NETWORK_PCSCF_HOST_L = SipConfigHelper.sip_local_ip;
        NETWORK_PCSCF_PORT_L = SipConfigHelper.sip_local_port;
        NETWORK_PCSCF_HOST_N = SipConfigHelper.sip_net_ip;
        NETWORK_PCSCF_PORT_N = SipConfigHelper.sip_net_port;

        L_IP = SipConfigHelper.local_ip;
        L_PORT = SipConfigHelper.local_port;

        PROJECT_PATH = "http://" + SipConfigHelper.net_ip + ":" + SipConfigHelper.net_port + "/railway_emergency/";
        DAZHOU_BASE_PATH = "http://" + SipConfigHelper.net_ip + ":" + SipConfigHelper.net_port + "/";
        BASE_PATH = "http://" + SipConfigHelper.net_ip + ":" + SipConfigHelper.net_port + "/";

        PROJECT_PATH_L = "http://" + SipConfigHelper.local_ip + ":" + SipConfigHelper.local_port + "/railway_emergency/";
        DAZHOU_BASE_PATH_L = "http://" + SipConfigHelper.local_ip + ":" + SipConfigHelper.local_port + "/";
        BASE_PATH_L = "http://" + SipConfigHelper.local_ip + ":" + SipConfigHelper.local_port + "/";

        PROJECT_PATH_N = "http://" + SipConfigHelper.net_ip + ":" + SipConfigHelper.net_port + "/railway_emergency/";
        DAZHOU_BASE_PATH_N = "http://" + SipConfigHelper.net_ip + ":" + SipConfigHelper.net_port + "/";
        BASE_PATH_N = "http://" + SipConfigHelper.net_ip + ":" + SipConfigHelper.net_port + "/";
    }

    public static void initConfig(INgnConfigurationService goal, Context context, String username, String ipphone) {
        Timber.Companion.e("initConfig方法被调用了");
        IDENTITY_IMPU = ipphone == null ? IDENTITY_IMPU : ipphone;
        IDENTITY_DISPLAY_NAME = username == null ? IDENTITY_IMPU : username;
        IDENTITY_CAMERA_GROUP = ipphone == null ? IDENTITY_CAMERA_GROUP : ipphone.replaceFirst("1", "8");
        goal.putString(
                IDENTITY_DISPLAY_NAME, SharedPreferenceUtil.getString(context, IDENTITY_DISPLAY_NAME, username));
        goal.putString(NgnConfigurationEntry.IDENTITY_IMPU,
                String.format("sip:%s@%s", IDENTITY_IMPU, NETWORK_REALM));
        goal.putString(NgnConfigurationEntry.IDENTITY_IMPI,
                SharedPreferenceUtil.getString(context, NgnConfigurationEntry.IDENTITY_IMPI, IDENTITY_IMPU));
        goal.putString(NgnConfigurationEntry.IDENTITY_PASSWORD,
                SharedPreferenceUtil.getString(context, NgnConfigurationEntry.IDENTITY_PASSWORD, IDENTITY_PASSWORD));
        goal.putString(NgnConfigurationEntry.NETWORK_REALM,
                SharedPreferenceUtil.getString(context, NgnConfigurationEntry.NETWORK_REALM, NETWORK_REALM));
        goal.putBoolean(NgnConfigurationEntry.NETWORK_USE_EARLY_IMS,
                SharedPreferenceUtil.getBoolean(context, NgnConfigurationEntry.NETWORK_USE_EARLY_IMS, false));
        goal.putString(NgnConfigurationEntry.NETWORK_PCSCF_HOST,
                SharedPreferenceUtil.getString(context, NgnConfigurationEntry.NETWORK_PCSCF_HOST, NETWORK_PCSCF_HOST));

        goal.putInt(NgnConfigurationEntry.NETWORK_PCSCF_PORT,
                SharedPreferenceUtil.getInt(context, NgnConfigurationEntry.NETWORK_PCSCF_PORT, NETWORK_PCSCF_PORT));
        goal.putBoolean(NgnConfigurationEntry.NETWORK_USE_WIFI,
                SharedPreferenceUtil.getBoolean(context, NgnConfigurationEntry.NETWORK_USE_WIFI, true));

        goal.putBoolean(NgnConfigurationEntry.NETWORK_USE_3G,
                SharedPreferenceUtil.getBoolean(context, NgnConfigurationEntry.NETWORK_USE_3G, true));
        goal.putInt(NgnConfigurationEntry.NETWORK_REGISTRATION_TIMEOUT,
                SharedPreferenceUtil.getInt(context, NgnConfigurationEntry.NETWORK_REGISTRATION_TIMEOUT, DEFAULT_NETWORK_REGISTRATION_TIMEOUT));

        goal.putBoolean(USE_SPEAKER, DEFAULT_USE_SPEAKER);
        // 固定值？
        goal.putInt(NgnConfigurationEntry.MEDIA_CODECS, NgnConfigurationEntry.DEFAULT_MEDIA_CODECS);

        goal.commit();

    }

    public static void updateConfig(INgnConfigurationService goal, String key, String value) {
        goal.putString(key, value);
        goal.commit();
    }

    public static void updateConfig(INgnConfigurationService goal, String key, int value) {
        goal.putInt(key, value);
        goal.commit();
    }

    /**
     * 判断TCP是否连接,请异步调用
     *
     * @param ip
     * @param port
     * @return
     */
    public static boolean isConnTcp(String ip, int port) {
        boolean isConn = false;
        try {
            Socket socket = new Socket();
            SocketAddress socAddress = new InetSocketAddress(ip, port);
            socket.connect(socAddress, 1000);//超时2秒
            isConn = socket.isConnected();
            socket.close();
            Log.e("Socket调试", "socket连接成功！ " + ip + ":" + port);
        } catch (Exception e) {
            isConn = false;
            Log.e("Socket调试", "socket连接失败！ " + ip + ":" + port);
        }
        return isConn;


//        try {
//            Socket socket = new Socket();
//            SocketAddress socAddress = new InetSocketAddress(ip, port);
//            //输入输出的超时时间
//            socket.setSoTimeout(1000);
//            //连接地址和连接超时时间
//            socket.connect(socAddress, 1500);
//
////            socket.connect(socAddress, 1000);//超时2秒
//            boolean is = socket.isConnected();
//            Log.e("socket", "VAL:" + is);
//            socket.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//        try {
//            //-c ping的次数，-w超时时间 秒
//            Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 1.5 " + ip);
//            int status = process.waitFor();
//            if (status == 0) {
//                //网络是通的
//                return true;
//            } else {
//                //网络不通
//                return false;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }


    }

    public static final void checkServerIp() {
        boolean result = isConnTcp(L_IP, L_PORT);
        if (!result) {
            // 外网
            BASE_PATH = BASE_PATH_N;
            PROJECT_PATH = PROJECT_PATH_N;
            DAZHOU_BASE_PATH = DAZHOU_BASE_PATH_N;
            NETWORK_PCSCF_HOST = NETWORK_PCSCF_HOST_N;
            NETWORK_PCSCF_PORT = NETWORK_PCSCF_PORT_N;
            Log.i(TAG, "外网登陆");
        } else {
            // 内网
            BASE_PATH = BASE_PATH_L;
            PROJECT_PATH = PROJECT_PATH_L;
            DAZHOU_BASE_PATH = DAZHOU_BASE_PATH_L;
            NETWORK_PCSCF_HOST = NETWORK_PCSCF_HOST_L;
            NETWORK_PCSCF_PORT = NETWORK_PCSCF_PORT_L;
            Log.i(TAG, "内网登陆");
        }
    }
}
